package com.sap.jcp.sm.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hsqldb.lib.StringUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sap.jcp.sm.dao.TrackDAO;
import com.sap.jcp.sm.dao.TrackDAOImpl;
import com.sap.jcp.sm.domain.Track;
import com.sap.jcp.sm.domain.User;
import com.sap.jcp.sm.util.MappingUtil;

public class TrackAction extends ActionSupport implements ModelDriven<Track>, SessionAware, RequestAware {

	Map sessionMap;
	Map request;
	private static final long serialVersionUID = 1L;
	private static final String LOGINSUCCESS = "loginsuccess";
	private Track track = new Track();
	private List<Track> trackList = new ArrayList<Track>();
	private TrackDAO trackDAO = new TrackDAOImpl();
	private String message;
	private List<User> userList = new ArrayList<User>();
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void validate() {
		if (StringUtil.isEmpty(track.getName())) {
			super.addActionError("Track Name required.");
		}
	}

	public Track getModel() {
		return track;
	}
	
	public String addTrack() {
		validate();
		if (getActionErrors() != null && !getActionErrors().isEmpty()) {
			return ERROR;
		}
		track.setDeleteFlag("false");
		setMessage("Track has been created successfully.");
		trackDAO.saveTrack(track);
		updateSessionTrack();
		return SUCCESS;
	}

	private void updateSessionTrack() {
		List<Track> trackList = trackDAO.listTrack();
		sessionMap.put("track", MappingUtil.convertTrackListToMap(trackList));
		sessionMap.remove("editTrackInfo");
	}
	public String editTrack()
	{
		Long trackId = (Long) request.get("trackId");
		Track trackInfo = trackDAO.getTrack(trackId);
		sessionMap.put("editTrackInfo", trackInfo);
		track.setDeleteFlag("false");
		return SUCCESS;
	}
	
	public String updateTrack()
	{
		validate();
		track.setDeleteFlag("false");
		if (getActionErrors() != null && !getActionErrors().isEmpty()) {
			return ERROR;
		}
		trackDAO.updateTrack(track);
		updateSessionTrack();
		return SUCCESS;
	}
	public String cancelEdit() {
		sessionMap.remove("editTrackInfo");
		return SUCCESS;
	}
	
	public String delete()
	{
		Long trackId = (Long)request.get("trackId");
		Track trackInfo = trackDAO.getTrack(trackId);
		trackInfo.setDeleteFlag("true");
		trackDAO.updateTrack(trackInfo);
		updateSessionTrack();
		return SUCCESS;
	}
	
	public String list() {
		trackList = trackDAO.listTrack();
		return SUCCESS;
	}
		
	public Track getTrack() {
		return track;
	}

	public void setTrack(Track track) {
		this.track = track;
	}


	public List<Track> getTrackList() {
		return trackList;
	}

	public void setTrackList(List<Track> trackList) {
		this.trackList = trackList;
	}
	@Override
	public void setSession(Map<String, Object> map) {
		this.sessionMap = map;
	}
	public Map<String, Object> getSession() {
		 return sessionMap;
	}

	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
		
	}

}
